
from myelin.server.lib.http import ParamValidators

from Service import Service


class Service(Service):
    
    def fetchParams(self, context, source, params, errors):
        
        defaults = context.defaults
        args = context.request.args
        config = context.event.data.config
        
        print "CONFIG PARAMS: ", config.params
        
        #print "args: %s" % args
        acct = context.account
        if acct.isAdmin:
            max = None
        elif acct.isAgent():
            max = config.params.get((None,'user-query-length')) or self.defaults['keywords-length']
            if max: max = int(max)
        else:
            max = config.params.get((None,'guest-query-length')) or self.defaults['keywords-length']
            if max: max = int(max)
         
        params['query'] = ParamValidators.String(
            'query',
            args,
            errors,
            min=1,
            max=max
        )
        
        #params['base-id'] = ParamValidators.String(
        #    'base-id',
        #    args,
        #    errors
        #)
        #
        ## 0 = direct children of this node only
        ## 1 = all nodes in site
        ## 2 = all children of this node only
        #if args.pop(u'quick-search',None):
        #    params['quick-search'] = u'1'
        #    params['range'] = args.pop('range', None) and 2 or 1 
        #else:
        #    params['range'] = ParamValidators.NumberRange(
        #        'range',
        #        args,
        #        errors,
        #        min=0,
        #        max=2,
        #        default=1
        #    )
        
        params['count'] = ParamValidators.NumberRange(
            'count',
            args,
            errors,
            min=int(config.params.get((None,'default-count-min')) or self.defaults['search-count-min']),
            max=int(config.params.get((None,'default-count-max')) or self.defaults['search-count-max']),
            default=int(config.params.get((None,'default-count')) or self.defaults['search-count'])
        )
        
        params['page'] = ParamValidators.NumberRange(
            'page',
            args,
            errors,
            min=0,
            default=0
        )
        
        #params['sort-by'] = ParamValidators.NumberRange(
        #    'sort-by',
        #    args,
        #    errors,
        #    max=len(self.sort_by),
        #    default=self.defaults.get('sort-by')
        #)
        #
        #params['sort-direction'] = ParamValidators.NumberRange(
        #    'sort-direction',
        #    args,
        #    errors,
        #    max=len(self.sort_order),
        #    default=self.defaults.get('sort-direction')
        #)
        
        if args.pop(u'quick-search',None):            
            locale = ParamValidators.String(
                'locale',
                args,
                errors
            )
            if locale:
                from nodetypes.lib import Utils
                locale = Utils.SplitLocale(locale)
                params['language'] = locale[0]
                #if len(locale) == 2:
                #    params['region'] = locale[1]
                #else:
                #    params['region'] == u''
            else:
                params['language'] = u''
                #params['region'] = u''
                            
            params['textindex-uri'] = config.defaultTextIndices
            
        else:
            params['language'] = ParamValidators.String(
                'language',
                args,
                errors
            )
            
            #params['region'] = ParamValidators.String(
            #    'region',
            #    args,
            #    errors
            #)
            
            data = ParamValidators.StringList(
                'textindex-uri',
                args,
                errors
            )
            
            if not data:
                params['textindex-uri'] = config.defaultTextIndices
            else:
                defaultIndices = {}
                searchIndices = config.textIndices
                for i in data:
                    if i in searchIndices:
                        defaultIndices[i] = searchIndices[i]
                if defaultIndices:
                    params['textindex-uri'] = defaultIndices
                else:
                    errors['textindex-uri'] = u'invalid-values'
                    params['textindex-uri'] = config.defaultTextIndices
                 
        
            print "DGU: ", params['textindex-uri']                   
        
        #params['efdFlag'] = ParamValidators.BitFlag(
        #    'efdFlag',
        #    args,
        #    errors
        #)
        #if params['efdFlag']:
        #    dsy = int(
        #        getattr(
        #            defaults,
        #            'default_search_start_year'
        #        ) or
        #        self.defaults['search-start-year']
        #    )
        #    params['efdYr'] = ParamValidators.NumberRange(
        #        'efdYr',
        #        args,
        #        errors,
        #        min=dsy,
        #        max=context.session.dateTime.year,
        #        default=dsy
        #    )            
        #    params['efdMon'] = ParamValidators.NumberRange(
        #        'efdMon',
        #        args,
        #        errors,
        #        min=1,
        #        max=12,
        #        default=1
        #    )
        
            #print "EFF DATE:"
            #print " %s" % params['efdYr']
            #print " %s" % params['efdMon']
        
        return params, errors
        